import os
import sys


def jpg2src(FILE_IN, VAR_NAME, FILE_OUT):
    if not os.path.isfile(FILE_IN):
        sys.exit(f'❌ 文件不存在: {FILE_IN}')

    with open(FILE_IN, 'rb') as f:
        blob = f.read()

    size = len(blob)

    with open(FILE_OUT, 'w', encoding='utf-8') as h:
        h.write(f'// 原始文件: {FILE_IN}\n')
        h.write(f'// 长度: {size} 字节\n')
        h.write('#include <pgmspace.h>\n\n')
        h.write(f'const uint8_t {VAR_NAME}[] PROGMEM = {{\n')

        # 每行 16 字节，方便阅读
        for idx, byte in enumerate(blob):
            if idx % 16 == 0:
                h.write('\n  ')
            h.write(f'0x{byte:02X},')

        h.write('\n};\n\n')
        h.write(f'const size_t {VAR_NAME}_LEN = {size};\n')

    print(f'✅ 已生成 {FILE_OUT}  ({size} 字节)')



for fn in ['da0', 'da1', 'da2', 'da3', 'da4', 'da5', 'da6', 'da7', 'da8', 'da9']:
    FILE_IN = f'{fn}.jpg'
    VAR_NAME = fn
    FILE_OUT = f'{fn}.jpg.h'
    jpg2src(FILE_IN, VAR_NAME, FILE_OUT)
